package cn.wh.common.dao;

import java.util.List;
import java.util.Map;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate4.HibernateCallback;
import org.springframework.orm.hibernate4.support.HibernateDaoSupport;

import cn.wh.common.Util.ConstantUtil;
import cn.wh.common.Util.PageInfoUtil;
import cn.wh.user.pojo.AAdmin;

/**
 * BaseDAO实现类
 * @author wanghao
 *
 */
public abstract class BaseDaoImpl<T> extends HibernateDaoSupport implements IBaseDao<T>{
	// 查询一条记录
	@Override
	public abstract T getOne(Map<String, Object> condMap);

	// 查询多条记录
	@Override
	public abstract List<T> getList(Map<String, Object> condMap, PageInfoUtil pageInfoUtil);
	
	// 添加一条记录
	@Override
	public int save(T t)
	{
		// 返回值就是主键
		return (int) this.getHibernateTemplate().save(t);
	}

	// 更新一条记录
	@Override
	public int update(T t)
	{
		try
		{
			this.getHibernateTemplate().update(t);
			return 1;
		} catch (Exception e)
		{
			ConstantUtil.LOGGER.error("更新记录失败,",e);
			throw e;
		}
	}
	
	//查询一条记录的返回值
	public T returnGetOne(String hql, Map<String,Object> paramMap) {
		// 调用hibernate查询一条记录
		return this.getHibernateTemplate().execute(new HibernateCallback<T>()
		{
			@Override
			public T doInHibernate(Session session) throws HibernateException
			{
				Query query = session.createQuery(hql.toString());
				// 设置查询有条件
				query.setProperties(paramMap);
				return (T) query.uniqueResult();
			}
		});
	}
	
	//查询多条记录的返回值 - 分页
	public List<T> returnGetListByPage(String hql,Map<String,Object> paramMap,PageInfoUtil pageInfoUtil) 
	{
		return this.getHibernateTemplate().execute(new HibernateCallback<List<T>>()
		{
			@Override
			public List<T> doInHibernate(Session session) throws HibernateException
			{
				// 先查询总记录数select count(*) from AAdmin where 1 = 1 and id = 1
				String countHql = "select count(*) " + hql.substring(hql.indexOf("from"),hql.indexOf("order by "));
				// 查询总记录数
				Query query = session.createQuery(countHql);
				// 设置查询有条件
				query.setProperties(paramMap);
				// 总记录数返回结果
				Long totalRecord = (Long) query.uniqueResult();
				// 设置总记录数
				pageInfoUtil.setTotalRecord(totalRecord.intValue());
				
				// 查询分页数据from AAdmin where 1 = 1 and id = 1 order by createDate desc limit ?,?
				Query query2 = session.createQuery(hql.toString());
				// 设置查询有条件
				query2.setProperties(paramMap);
				// 设置分页条件
				// limit第一个问号：当前页条数
				query2.setFirstResult(pageInfoUtil.getCurrentRecord());
				// limit第二个问号：每页多少条
				query2.setMaxResults(pageInfoUtil.getPageSize());
				
				return query2.list();
			}
	    });
	}
	
	//查询多条记录的返回值 - 不分页
		public List<T> returnGetListNoPage(String hql,Map<String,Object> paramMap) 
		{
			return this.getHibernateTemplate().execute(new HibernateCallback<List<T>>()
			{
				@Override
				public List<T> doInHibernate(Session session) throws HibernateException
				{
					Query query = session.createQuery(hql.toString());
					// 设置查询有条件
					query.setProperties(paramMap);
					return query.list();
				}
		    });
		}
}
